//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Installer;
using DotNetNuke.Services.Installer.Packages;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI;
using DotNetNuke.UI.Modules;
using DotNetNuke.UI.WebControls;
using DotNetNuke.Services.Installer.Writers;
using System.IO;
namespace DotNetNuke.Modules.Admin.Extensions
{
	partial class EditExtension : ModuleUserControlBase
	{
		private Control _Control;
		private PackageInfo _Package = null;
		protected bool IsSuperTab {
			get { return (this.ModuleContext.PortalSettings.ActiveTab.IsSuperTab); }
		}
		public string Mode {
			get { return Convert.ToString(ModuleContext.Settings["Extensions_Mode"]); }
		}
		protected PackageInfo Package {
			get {
				if (_Package == null) {
					if (PackageID == Null.NullInteger) {
						_Package = new PackageInfo();
					} else {
						_Package = PackageController.GetPackage(PackageID);
					}
				}
				return _Package;
			}
		}
		protected IPackageEditor PackageEditor {
			get {
				if (_Control == null) {
					if (Package != null) {
						PackageType _PackageType = PackageController.GetPackageType(Package.PackageType);
						if ((_PackageType != null) && (!string.IsNullOrEmpty(_PackageType.EditorControlSrc))) {
							_Control = ControlUtilities.LoadControl<Control>(this, _PackageType.EditorControlSrc);
						}
					}
				}
				return _Control as IPackageEditor;
			}
		}
		public int PackageID {
			get {
				int _PackageID = Null.NullInteger;
				if ((Request.QueryString["PackageID"] != null)) {
					_PackageID = Int32.Parse(Request.QueryString["PackageID"]);
				}
				return _PackageID;
			}
		}
		protected PropertyEditorMode ViewMode {
			get {
				PropertyEditorMode _ViewMode = PropertyEditorMode.View;
				if (Request.IsLocal && IsSuperTab) {
					_ViewMode = PropertyEditorMode.Edit;
				}
				return _ViewMode;
			}
		}
		private void BindData()
		{
			fldEmail.ValidationExpression = Common.Globals.glbEmailRegEx;
			trLanguagePackType.Visible = false;
			switch (Mode) {
				case "All":
					lblHelp.Text = Localization.GetString("EditHelp", this.LocalResourceFile);
					cmdUpdate.Text = Localization.GetString("cmdUpdate", this.LocalResourceFile);
					break;
				case "LanguagePack":
					lblHelp.Text = Localization.GetString("EditLanguageHelp", this.LocalResourceFile);
					cmdUpdate.Text = Localization.GetString("cmdUpdateLanguage", this.LocalResourceFile);
					break;
				case "Module":
					lblHelp.Text = Localization.GetString("EditModuleHelp", this.LocalResourceFile);
					cmdUpdate.Text = Localization.GetString("cmdUpdateModule", this.LocalResourceFile);
					break;
				case "Skin":
					lblHelp.Text = Localization.GetString("EditSkinHelp", this.LocalResourceFile);
					cmdUpdate.Text = Localization.GetString("cmdUpdateSkin", this.LocalResourceFile);
					break;
			}
			if (ModuleContext.PortalSettings.ActiveTab.IsSuperTab) {
				lblTitle.Text = string.Format(Localization.GetString("HostHeading", LocalResourceFile), Package.FriendlyName);
			} else {
				lblTitle.Text = string.Format(Localization.GetString("AdminHeading", LocalResourceFile), Package.FriendlyName);
			}
			cmdPackage.Visible = IsSuperTab;
			cmdUpdate.Visible = IsSuperTab;
			if (Package != null) {
				if (PackageEditor == null || PackageID == Null.NullInteger) {
					dshExtension.Visible = false;
					dshPackage.Visible = false;
					tblExtension.Visible = false;
				} else {
					phEditor.Controls.Clear();
					phEditor.Controls.Add(PackageEditor as Control);
					IModuleControl moduleControl = PackageEditor as IModuleControl;
					if (moduleControl != null) {
						moduleControl.ModuleContext.Configuration = this.ModuleContext.Configuration;
					}
					if (PackageEditor != null) {
						PackageEditor.PackageID = PackageID;
						PackageEditor.Initialize();
					}
				}
				ctlExtension.EditMode = ViewMode;
				ctlExtension.DataSource = Package;
				ctlExtension.DataBind();
				if (Mode != "All") {
					fldPackageType.Visible = false;
				}
				//Determine if Package is ready for packaging
                PackageWriterBase Writer = PackageWriterFactory.GetWriter(Package);
				cmdPackage.Visible = Directory.Exists(Path.Combine(DotNetNuke.Common.Globals.ApplicationMapPath, Writer.BasePath));

				cmdDelete.Visible = IsSuperTab && (!Package.IsSystemPackage) && (PackageController.CanDeletePackage(Package, ModuleContext.PortalSettings));
				ctlAudit.Entity = Package;
			}
		}
		private void UpdatePackage(bool displayMessage)
		{
			if (ctlExtension.IsValid && ctlExtension.IsDirty) {
				PackageInfo package = ctlExtension.DataSource as PackageInfo;
				if (package != null) {
					PackageController.UpdatePackage(package);
				}
				if (displayMessage) {
					DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("PackageUpdated", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
				}
			}
			if (PackageEditor != null) {
				PackageEditor.UpdatePackage();
			}
		}
		protected override void OnInit(System.EventArgs e)
		{
			base.OnInit(e);
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdPackage.Click += new EventHandler(cmdPackage_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);

			BindData();

		}
		private void Package_Updated(object sender, EventArgs e)
		{
		}
		protected void cmdCancel_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(Common.Globals.NavigateURL());
		}
		protected void cmdDelete_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(Util.UnInstallURL(ModuleContext.TabId, PackageID));
		}
		protected void cmdPackage_Click(object sender, System.EventArgs e)
		{
			try {
				UpdatePackage(false);
				Response.Redirect(Util.PackageWriterURL(ModuleContext, PackageID));
			} catch (Exception ex) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
			}
		}
		protected void cmdUpdate_Click(object sender, System.EventArgs e)
		{
			try {
				UpdatePackage(true);
				Response.Redirect(Request.RawUrl, true);
			} catch (Exception ex) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
			}
		}
	}
}
